/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package EnigmaUtils;
import Actors.Actor;
import Floors.Floor;
import Items.Item;
import Model.ActorManager;
import Model.Case;
import Model.Inventory;
import Model.Plateau;
import Stones.Stone;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import java.util.List;
import java.util.Iterator;

/**
 *
 * @author Snowangelic
 */

public class LevelGenerator
{
   org.jdom.Document document;
   Element racine;

   public LevelGenerator(File xmlFile){
      //On crée une instance de SAXBuilder
       if(xmlFile==null) System.out.println("OK");
      SAXBuilder sxb = new SAXBuilder();
      try
      {
         document = sxb.build(xmlFile);
      }
      catch(Exception e){}

      //On initialise un nouvel élément racine avec l'élément racine du document.
      if(document!=null)
        racine = document.getRootElement();

      else System.out.println("Merde");
   }

   public void generateLevel(Inventory inventory,ActorManager actorManager,Plateau plateau){
       inventory.clear();
       actorManager.clear();
       plateau.clear();

       ActorFactory actorFactory=new ActorFactory();
       ItemFactory itemFactory=new ItemFactory();
       FloorFactory floorFactory=new FloorFactory();
       StoneFactory stoneFactory=new StoneFactory();

       //--- Gestion des acteurs ---
       List listActors = racine.getChildren("Actor");
       Iterator i = listActors.iterator();
       while(i.hasNext()){
           Element courant = (Element)i.next();
           Actor newActor=actorFactory.generateActor(courant.getChildText("Type"));
           actorManager.addActor(newActor);
       }

       //--- Gestion de l'inventaire ---
       List listItems = racine.getChildren("InventoryItem");
       Iterator j = listItems.iterator();
       while(j.hasNext()){
           Element courant = (Element)j.next();
           Item newItem=itemFactory.generateItem(courant.getChildText("Type"));
           if(newItem !=null) newItem.setInventory(inventory);
           inventory.addItem(newItem);
       }

       //--- Gestion du plateau ---
       Element plate = racine.getChild("Plateau");
       int width=Integer.parseInt(plate.getChildText("Width"));
       int height=Integer.parseInt(plate.getChildText("Height"));
       plateau.setPlaneDimension(width, height);

       List listCases = plate.getChildren("Cases");
       Iterator k = listCases.iterator();
       int count=0;
       while(k.hasNext()){
           Element courant = (Element)k.next();
           Item caseItem=itemFactory.generateItem(courant.getChildText("ItemType"));
           if(caseItem!=null)caseItem.setInventory(inventory);
           Floor caseFloor=floorFactory.generateFloor(courant.getChildText("FloorType"));
           Stone caseStone=stoneFactory.generateStone(courant.getChildText("StoneType"));
           Case myCase=new Case(plateau,count%width,count/width,caseFloor,caseStone,caseItem);
           plateau.addCase(myCase);
           count++;
       }

   }
}